/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * misc.c
 *
 * Copyright (C) 2008, 2009 Lingtao Pan
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, 
 * Boston, MA 02111-1307, USA.
 */


#include "common.h"

#ifdef dprintf
 #undef dprintf
 #define dprintf 
#endif

void
ftp_log_gui (gftp_logging_level level, gftp_request * request, 
             const char *string, ...)
{
    char *logstr;
    va_list argp;
    gftp_log *newlog;
    int free_logstr;

    GtkTextBuffer * textbuf;
    GtkTextIter iter;
    const char *descr;
    char *utf8_str;
    size_t destlen;
    GtkTextMark *end_pos;

    va_start (argp, string);
    if (strcmp (string, "%s") == 0) {
        logstr = va_arg (argp, char *);
        free_logstr = 0;
    } else {
        logstr = g_strdup_vprintf (string, argp);
        free_logstr = 1;
    }
    va_end (argp);

    destlen = strlen (logstr);
    assert (destlen > 0 && destlen < 1000);
    
    if (!g_utf8_validate (logstr, -1, NULL)) {
        if ((utf8_str = gftp_string_to_utf8 (request, logstr, &destlen)) != NULL) 
        {
            if (free_logstr)
                g_free (logstr);
            else
                free_logstr = 1;

            logstr = utf8_str;
        }
    }
    assert (destlen > 0 && destlen < 1000);

    if ( ! pthread_equal(pthread_self (), main_thread_id) ) {
        newlog = g_malloc0 (sizeof (*newlog));
        newlog->type = level;
        if (free_logstr)
            newlog->msg = logstr;
        else
            newlog->msg = g_strdup (logstr);
        
        pthread_mutex_lock (&log_mutex);
        gftp_file_transfer_logs = g_list_append (gftp_file_transfer_logs, newlog);
        pthread_mutex_unlock (&log_mutex);
        return;
    }

    switch (level) {
    case gftp_logging_send:
        descr = "send";
        break;
    case gftp_logging_recv:
      descr = "recv";
      break;
    case gftp_logging_error:
        descr = "error";
        break;
    default:
        descr = "misc";
        break;
    }
    
    textbuf = gtk_text_view_get_buffer (GTK_TEXT_VIEW (gftp_interface->logwdw));
    /* len = gtk_text_buffer_get_char_count (textbuf); */
    gtk_text_buffer_get_end_iter (textbuf, &iter);
    gtk_text_buffer_insert_with_tags_by_name (textbuf, &iter, logstr, -1,
                                              descr, NULL);
    
    end_pos = gtk_text_buffer_get_mark (textbuf, "end");
    if (end_pos != NULL)
        gtk_text_view_scroll_mark_onscreen (GTK_TEXT_VIEW (gftp_interface->logwdw),
                                            end_pos);
    
    if (free_logstr)
        g_free (logstr);
}

int
get_selected_protocol ()
{
    char *text;
    int i;

    text = gtk_combo_box_get_active_text (GTK_COMBO_BOX
                                        (gftp_interface->optionmenu));

    for (i = 0; gftp_protocols[i].name; i++) {
        if (strcmp (gftp_protocols[i].name, text) == 0)
            break;
    }
    
    return i;
}

void
display_cached_logs ()
{
    gftp_log * templog;
    GList * templist; 

    pthread_mutex_lock (&log_mutex);
    templist = gftp_file_transfer_logs;
    while (templist != NULL) { 
        templog = (gftp_log *) templist->data;
        ftp_log_gui (templog->type, NULL, "%s", templog->msg);
        g_free (templog->msg);
        g_free (templog); 
        templist->data = NULL;
        templist = templist->next;
    }
    g_list_free (gftp_file_transfer_logs);
    gftp_file_transfer_logs = NULL;
    pthread_mutex_unlock (&log_mutex);
}
